#include "aw9523b.h"

#include "pico/stdlib.h"
#include "hardware/i2c.h"


/* Macro Definition
********************************************************************************************************/
//#define AW9523B_ADDR 0xB6    //  1 0 1 1 0 AD1 AD0 RW, AD1:1 AD0:1
#define AW9523B_ADDR 0x5B    //  1 0 1 1 0 AD1 AD0 RW, AD1:1 AD0:1

// REG defination
#define INPUT_PORT0  0x00    // R
#define INPUT_PORT1  0x01    // R
#define OUTPUT_PORT0 0x02    // RW
#define OUTPUT_PORT1 0x03    // RW
#define CONFIG_PORT0 0x04    // RW
#define CONFIG_PORT1 0x05    // RW
#define INT_PORT0    0x06    // RW
#define INT_PORT1    0x07    // RW
#define REG_ID       0x10    // R
#define REG_CTL      0x11    // WR, Global Ctrl
#define MODE_PORT0   0x12    // WR
#define MODE_PORT1   0x13    // WR
#define DIM0         0x20    // W, P1_0 LED current control
#define DIM1         0x21    // W, P1_1 LED current control
#define DIM2         0x22    // W, P1_2 LED current control
#define DIM3         0x23    // W, P1_3 LED current control
#define DIM4         0x24    // W, P0_0 LED current control
#define DIM5         0x25    // W, P0_1 LED current control
#define DIM6         0x26    // W, P0_2 LED current control
#define DIM7         0x27    // W, P0_3 LED current control
#define DIM8         0x28    // W, P0_4 LED current control
#define DIM9         0x29    // W, P0_5 LED current control
#define DIM10        0x2A    // W, P0_6 LED current control
#define DIM11        0x2B    // W, P0_7 LED current control
#define DIM12        0x2C    // W, P1_4 LED current control
#define DIM13        0x2D    // W, P1_5 LED current control
#define DIM14        0x2E    // W, P1_6 LED current control
#define DIM15        0x2F    // W, P1_7 LED current control
#define SW_RSTN      0x7F    // W, Soft reset


#define AW9523B_I2C_SDA_PIN 18
#define AW9523B_I2C_SCL_PIN 19
#define AW9523B_RST_PIN     28
#define AW9523B_I2C_PORT    i2c1
#define AW9523B_I2C_BAUD    (100 * 1000)

/* Function Declaration
********************************************************************************************************/
static void    write_reg(uint8_t reg, uint8_t val);
static uint8_t read_reg(uint8_t reg);

/* Structure Definition
********************************************************************************************************/

/* Global Variables
********************************************************************************************************/

/* Public Functions
********************************************************************************************************/
void aw9523b_init(void)
{
    i2c_init(AW9523B_I2C_PORT, AW9523B_I2C_BAUD);
    gpio_set_function(AW9523B_I2C_SDA_PIN, GPIO_FUNC_I2C);
    gpio_set_function(AW9523B_I2C_SCL_PIN, GPIO_FUNC_I2C);
    gpio_pull_up(AW9523B_I2C_SDA_PIN);
    gpio_pull_up(AW9523B_I2C_SCL_PIN);

    // Reset pin
    gpio_init(AW9523B_RST_PIN);
    gpio_set_dir(AW9523B_RST_PIN, GPIO_OUT);
    gpio_put(AW9523B_RST_PIN, 1);

    sleep_ms(100);

    // reset
    write_reg(SW_RSTN, 0x00);

    uint8_t id = read_reg(REG_ID);

    // Port out
    write_reg(CONFIG_PORT0, 0x00);
    write_reg(CONFIG_PORT1, 0x00);

    // Port LED Mode
    write_reg(MODE_PORT0, 0x00);
    write_reg(MODE_PORT1, 0x00);

    // Out level
    write_reg(OUTPUT_PORT0, 0x00);
    write_reg(OUTPUT_PORT0, 0x00);

    write_reg(DIM0, 0xFF);
    write_reg(DIM1, 0xFF);
    write_reg(DIM2, 0xFF);
    write_reg(DIM3, 0xFF);
    // write_reg(DIM4, 0xFF);
    // write_reg(DIM5, 0xFF);
}


/* Private Functions
********************************************************************************************************/
static void write_reg(uint8_t reg, uint8_t val)
{
    uint8_t buf[2] = {0};

    buf[0] = reg;
    buf[1] = val;
    i2c_write_blocking(AW9523B_I2C_PORT, AW9523B_ADDR, buf, 2, false);
}

static uint8_t read_reg(uint8_t reg)
{
    uint8_t val = 0;

    i2c_write_blocking(AW9523B_I2C_PORT, AW9523B_ADDR, &reg, 1, true);
    i2c_read_blocking(AW9523B_I2C_PORT, AW9523B_ADDR, &val, 1, false);

    return val;
}


/* End Of File
********************************************************************************************************/
